package com.springboot.controller;

import com.springboot.dto.Doctor;
import com.springboot.dto.Doctor2;
import com.springboot.model.*;
import com.springboot.service.OrderDoctorService;
import com.springboot.service.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.springboot.service.PatientOrderViewService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.websocket.server.PathParam;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 本控制台用于响应 病人预约模块
 */
@Controller
public class PatientOrderController {
    @Autowired
    PatientOrderViewService patientOrderViewService;

    @Autowired
    OrderDoctorService orderDoctorService;

    @Autowired
    UserLoginService userLoginService;

    /** (该业务待定)
     * 点击科室，返回该科室的医生列表
     *
     * @return 医生列表
     */
    @CrossOrigin
    @RequestMapping(value = "/patientOrderView")
    public @ResponseBody
    ArrayList<DoctorInfoForm> patientOrderView(@RequestParam("field") String field){
        System.out.println("给患者展示预约页面");
        try {
            ArrayList<DoctorFieldForm> doctorFieldForms = patientOrderViewService.findDoctorsByField(field);
            ArrayList<DoctorInfoForm> doctorInfoForms = new ArrayList<>();
            System.out.println(doctorFieldForms.toString());
            System.out.println("正在获取医生信息");
            for(int i=0;i<doctorFieldForms.size();i++)
            {
                DoctorFieldForm doctorFieldForm = doctorFieldForms.get(i);
                DoctorInfoForm doctorInfoForm = patientOrderViewService.findDoctorsById(doctorFieldForm.getDoctorId());
                doctorInfoForms.add(doctorInfoForm);
            }
            System.out.println("成功获取医生信息");
            return doctorInfoForms;
        }
        catch (Exception e)
        {
            return null;
        }
    }

    /**
     * 当患者点击预约时，前端会持续访问控制台，询问每天该患者是否可预约该医生
     * @param doctorId
     * @return  True/False
     */
    @CrossOrigin
    @RequestMapping(value = "/doctorOrderTimeView")
    @ResponseBody
    public Map<String,String> doctorOrderTimeView(
            @RequestParam("unitphone") String doctorTel,
            @RequestParam("bookdate") String bookdate,
            @RequestParam("doctorId") Integer doctorId) {
        System.out.println(doctorId+"123");
        System.out.println("给患者展示预约页面");
        System.out.println(doctorTel);
        System.out.println(bookdate);
        Map<String,String> result = new HashMap<>();
        result.put("success","False");
        try {
            ArrayList<DoctorAppointSet> doctorAppointSets = patientOrderViewService.findOrderTimeByDoctorId(doctorId);
            for(int i=0;i<doctorAppointSets.size();i++)
            {
                DoctorAppointSet doctorAppointSet = doctorAppointSets.get(i);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String bdt = doctorAppointSet.getBookDate();
                Date  date = sdf.parse(bookdate);
                String bookdate2 = sdf.format(date);
                //检查是否重复预约
                if(bdt.equals(bookdate2))
                {
                    System.out.println(bdt+"该天可预约");
                    result.clear();
                    java.sql.Date bookDate = new java.sql.Date(date.getTime());
                    Integer patientId = userLoginService.findUserByTel(doctorTel).getUserId();
                    OrderForm orderForm = new OrderForm(patientId, doctorId, bookDate);
                    if(orderDoctorService.checkOrderForm(orderForm)==1)
                    {
                        result.put("messege","您已经预约过该天，请勿重复预约");
                        result.put("success","False");
                    }
                    else {
                        result.put("success","True");
                    }
                }
            }
        }
        catch (Exception e)
        {
            result.put("success","False");
        }
        System.out.println(bookdate);
        System.out.println(result);
        //result.clear();
        //result.put("success","True");
        return result;
    }

    /**
     * 在预约页面，返回所有医生信息
     * @return
     */
    @CrossOrigin
    @RequestMapping(value = "/patientOrderView2")
    public @ResponseBody
    ArrayList<Doctor2> patientOrderView2(@RequestParam("flag") Integer flag,
                                         @RequestParam("searchText") String searchText){
        System.out.println("给患者展示预约页面");
        if(flag==0)
        {
            try {
                ArrayList<DoctorFieldForm> doctorFieldForms = patientOrderViewService.findAllDoctorField();
                System.out.println("正在获取医生信息");
                ArrayList<Doctor2> doctors2 = new ArrayList<>();
                System.out.println("医生数量："+doctorFieldForms.size());
                for(int i=0;i<doctorFieldForms.size();i++)
                {
                    DoctorFieldForm doctorFieldForm = doctorFieldForms.get(i);
                    DoctorInfoForm doctorInfoForm = new DoctorInfoForm();
                    try {
                        doctorInfoForm = patientOrderViewService.findDoctorsById(doctorFieldForm.getDoctorId());
                    }
                    catch (Exception e)
                    {
                        System.out.println("数据库中医生信息不一致");
                    }
                    UserLoginForm userLoginForm = new UserLoginForm();
                    try {
                        userLoginForm = userLoginService.findUserByTel(doctorInfoForm.getDoctorTel());
                    }
                    catch (Exception e)
                    {
                        System.out.println("数据库中医生信息不一致");
                    }
                    Doctor2 doctor2 = new Doctor2();
                    doctor2.setDoctorInfoForm(doctorInfoForm);
                    doctor2.setDoctorFieldForm(doctorFieldForm);
                    doctor2.setDoctorPhoto(userLoginForm.getLogoLink());
                    doctors2.add(doctor2);
                }
                System.out.println("成功获取医生信息");
                return doctors2;
            }
            catch (Exception e)
            {
                return null;
            }
        }
        //模糊查询
        else {
            ArrayList<DoctorInfoForm> doctorInfoForms = patientOrderViewService.findDoctorInfoFormByNameOrRank(searchText);
            ArrayList<Doctor2> doctor2s = new ArrayList<>();
            for(int i=0;i<doctorInfoForms.size();i++)
            {
                DoctorInfoForm doctorInfoForm = doctorInfoForms.get(i);
                Integer doctorId = doctorInfoForm.getDoctorId();
                DoctorFieldForm doctorFieldForm = patientOrderViewService.findDoctorFieldByDoctorId(doctorId);
                UserLoginForm userLoginForm = new UserLoginForm();
                try {
                    userLoginForm = userLoginService.findUserByTel(doctorInfoForm.getDoctorTel());
                }
                catch (Exception e)
                {
                    System.out.println("数据库中医生信息不一致");
                }
                Doctor2 doctor2 = new Doctor2();
                doctor2.setDoctorInfoForm(doctorInfoForm);
                doctor2.setDoctorFieldForm(doctorFieldForm);
                doctor2.setDoctorPhoto(userLoginForm.getLogoLink());
                doctor2s.add(doctor2);
            }
            return doctor2s;
        }

    }
}
